package controller

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/sessions"

	"im/model"
	"im/requests"
	"im/responses"
	"im/service"
)

type FriendsController struct {
	Session *sessions.Session
	Ctx     iris.Context
	Base
}

// swagger:route POST /friends friends post-friends
// 添加好友/关注
//
// 添加好友/关注, 去掉了type字段,不支持黑名单
// 好友是单向的,当添加好友时可以直接成功, 当双方同时关注时为双向好友,这个时候才能私聊
//   Security:
//   - jwt_key:
//   Responses:
//     200: ModifyFriendStatus
func (c *FriendsController) Post() {
	var (
		param requests.PostFriends
		user  = c.Ctx.Values().Get("user").(model.Users)
	)
	_ = c.Ctx.ReadJSON(&param)
	err := service.NewUsersFriends().Focus(user, param)
	if err != nil {
		c.SendCry(err)
		return
	}
	friendMap, err := service.NewUsersFriends().GetFriendStatus(user.Id, param.FriendId)
	status := 0
	if st, ok := friendMap[param.FriendId]; ok {
		status = st
	}
	c.SendSmile(responses.FriendStatus{status})
}

// swagger:route Delete /friends/{id} friends delete-friends
// 取消关注
//
// 取消关注
//   Security:
//   - jwt_key:
//   Responses:
//     200: ModifyFriendStatus
func (c *FriendsController) DeleteBy(friendId int64) {
	var (
		param requests.PostFriends
		user  = c.Ctx.Values().Get("user").(model.Users)
	)
	_ = c.Ctx.ReadJSON(&param)
	err := service.NewUsersFriends().UnFocus(user.Id, friendId)
	if err != nil {
		c.SendCry(err)
		return
	}
	friendMap, err := service.NewUsersFriends().GetFriendStatus(user.Id, param.FriendId)
	status := 0
	if st, ok := friendMap[param.FriendId]; ok {
		status = st
	}
	c.SendSmile(responses.FriendStatus{status})
}

// swagger:route GET /friends/followers friends get-friends-followers
// 获取粉丝(关注我的人)
//
// 获取粉丝(关注我的人)
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetUserWithFriendStatus
func (c *FriendsController) GetFollowers() {
	var resp = []responses.GetUserWithFriendStatus{}

	var param = requests.Pagination{
		Page:  1,
		Limit: 100,
	}
	_ = c.Ctx.ReadQuery(&param)
	var user = c.Ctx.Values().Get("user").(model.Users)
	list, err := service.NewUsersFriends().ListFollowers(user.Id, param)
	if err != nil {
		c.SendCry(err)
		return
	}
	var friendIdList = []int64{}
	for _, row := range list {
		friendIdList = append(friendIdList, row.Id)
	}
	friendMap, err := service.NewUsersFriends().GetFriendStatus(user.Id, friendIdList...)
	for _, mv := range list {
		status := 0
		if st, ok := friendMap[mv.Id]; ok {
			status = st
		}
		resp = append(resp, responses.GetUserWithFriendStatus{
			UsersVisually: mv,
			FriendStatus:  responses.FriendStatus{FriendStatus: status},
		})
	}
	c.SendSmile(resp)
}

// swagger:route GET /friends/focus friends get-friends-focus
// 获取关注列表(我关注的人)
//
// 获取关注列表(我关注的人)
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetUserWithFriendStatus
func (c *FriendsController) GetFocus() {
	var resp = []responses.GetUserWithFriendStatus{}
	var param = requests.Pagination{
		Page:  1,
		Limit: 100,
	}
	_ = c.Ctx.ReadQuery(&param)
	var user = c.Ctx.Values().Get("user").(model.Users)
	list, err := service.NewUsersFriends().ListFocus(user.Id, param)
	if err != nil {
		c.SendCry(err)
		return
	}
	var friendIdList = []int64{}
	for _, row := range list {
		friendIdList = append(friendIdList, row.Id)
	}
	friendMap, err := service.NewUsersFriends().GetFriendStatus(user.Id, friendIdList...)
	for _, mv := range list {
		status := 0
		if st, ok := friendMap[mv.Id]; ok {
			status = st
		}
		resp = append(resp, responses.GetUserWithFriendStatus{
			UsersVisually: mv,
			FriendStatus:  responses.FriendStatus{FriendStatus: status},
		})
	}
	c.SendSmile(list)
}
